/*     */ package com.zimbra.cs.dav.property;
/*     */ 
/*     */ import com.zimbra.common.calendar.ICalTimeZone;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZComponent;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Config;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.dav.DavContext;
/*     */ import com.zimbra.cs.dav.DavElements;
/*     */ import com.zimbra.cs.dav.DavException;
/*     */ import com.zimbra.cs.dav.resource.CalendarObject;
/*     */ import com.zimbra.cs.dav.resource.Principal;
/*     */ import com.zimbra.cs.dav.resource.UrlNamespace;
/*     */ import com.zimbra.cs.mailbox.Flag;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.calendar.Util;
/*     */ import java.io.CharArrayWriter;
/*     */ import java.io.IOException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Collection;
/*     */ import org.dom4j.DocumentHelper;
/*     */ import org.dom4j.Element;
/*     */ import org.dom4j.QName;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class CalDavProperty
/*     */   extends ResourceProperty
/*     */ {
/*     */   private static final String INDIVIDUAL = "INDIVIDUAL";
/*     */   private static final String RESOURCE = "RESOURCE";
/*     */   private static final String ROOM = "ROOM";
/*     */   private static final String UNKNOWN = "UNKNOWN";
/*     */   
/*     */   public static ResourceProperty getSupportedCalendarComponentSet(MailItem.Type view)
/*     */   {
/*  60 */     return new SupportedCalendarComponentSet(view);
/*     */   }
/*     */   
/*     */   public static ResourceProperty getSupportedCalendarData() {
/*  64 */     return new SupportedCalendarData();
/*     */   }
/*     */   
/*     */   public static ResourceProperty getSupportedCollationSet() {
/*  68 */     return new SupportedCollationSet();
/*     */   }
/*     */   
/*     */   public static ResourceProperty getCalendarData(CalendarObject obj) {
/*  72 */     return new CalendarData(obj);
/*     */   }
/*     */   
/*     */   public static ResourceProperty getCalendarHomeSet(String user) {
/*  76 */     return new CalendarHomeSet(user);
/*     */   }
/*     */   
/*     */   public static ResourceProperty getScheduleInboxURL(String user) {
/*  80 */     return new ScheduleInboxURL(user);
/*     */   }
/*     */   
/*     */   public static ResourceProperty getScheduleOutboxURL(String user) {
/*  84 */     return new ScheduleOutboxURL(user);
/*     */   }
/*     */   
/*     */   public static ResourceProperty getCalendarUserAddressSet(Collection<String> addrs) {
/*  88 */     return new CalendarUserAddressSet(addrs);
/*     */   }
/*     */   
/*     */   public static ResourceProperty getCalendarFreeBusySet(String user, Collection<Folder> folders) {
/*  92 */     return new CalendarFreeBusySet(user, folders);
/*     */   }
/*     */   
/*     */   public static ResourceProperty getCalendarUserType(Principal p) {
/*  96 */     return new CalendarUserType(p);
/*     */   }
/*     */   
/*     */   public static ResourceProperty getCalendarTimezone(Account a) {
/* 100 */     return new CalendarTimezone(a);
/*     */   }
/*     */   
/*     */   protected CalDavProperty(QName name) {
/* 104 */     super(name);
/* 105 */     setProtected(true);
/*     */   }
/*     */   
/*     */   public static enum CalComponent {
/* 109 */     VCALENDAR,  VEVENT,  VTODO,  VJOURNAL,  VTIMEZONE,  VFREEBUSY;
/*     */     
/*     */     private CalComponent() {} }
/*     */   
/* 113 */   public static CalComponent getCalComponent(String comp) { return CalComponent.valueOf(comp); }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private static class SupportedCalendarComponentSet
/*     */     extends CalDavProperty
/*     */   {
/*     */     public SupportedCalendarComponentSet(MailItem.Type view)
/*     */     {
/* 145 */       super();
/* 146 */       super.setAllowSetOnCreate(true);
/* 147 */       ArrayList<CalDavProperty.CalComponent> comps = new ArrayList();
/* 148 */       if (view == MailItem.Type.APPOINTMENT) {
/* 149 */         comps.add(CalDavProperty.CalComponent.VEVENT);
/* 150 */       } else if (view == MailItem.Type.TASK) {
/* 151 */         comps.add(CalDavProperty.CalComponent.VTODO);
/*     */       }
/*     */       else {
/* 154 */         comps.add(CalDavProperty.CalComponent.VEVENT);
/* 155 */         comps.add(CalDavProperty.CalComponent.VTODO);
/*     */       }
/*     */       
/*     */ 
/* 159 */       comps.add(CalDavProperty.CalComponent.VFREEBUSY);
/*     */       
/* 161 */       for (CalDavProperty.CalComponent comp : comps) {
/* 162 */         Element e = DocumentHelper.createElement(DavElements.E_COMP);
/* 163 */         e.addAttribute("name", comp.name());
/* 164 */         this.mChildren.add(e);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private static class SupportedCalendarData extends CalDavProperty {
/*     */     public SupportedCalendarData() {
/* 171 */       super();
/* 172 */       Element e = DocumentHelper.createElement(DavElements.E_CALENDAR_DATA);
/* 173 */       e.addAttribute("content-type", "text/calendar");
/* 174 */       e.addAttribute("version", "2.0");
/* 175 */       this.mChildren.add(e);
/*     */     }
/*     */   }
/*     */   
/*     */   private static class SupportedCollationSet extends CalDavProperty
/*     */   {
/*     */     public SupportedCollationSet() {
/* 182 */       super();
/* 183 */       Element e = DocumentHelper.createElement(DavElements.E_SUPPORTED_COLLATION);
/* 184 */       e.setText("i;ascii-casemap");
/* 185 */       this.mChildren.add(e);
/* 186 */       e = DocumentHelper.createElement(DavElements.E_SUPPORTED_COLLATION);
/* 187 */       e.setText("i;octet");
/* 188 */       this.mChildren.add(e);
/*     */     }
/*     */   }
/*     */   
/*     */   private static class CalendarData extends CalDavProperty {
/*     */     CalendarObject rs;
/*     */     
/* 195 */     public CalendarData(CalendarObject calobj) { super();
/* 196 */       this.rs = calobj;
/*     */     }
/*     */     
/*     */     public Element toElement(DavContext ctxt, Element parent, boolean nameOnly)
/*     */     {
/* 201 */       if (getStringValue() == null)
/*     */         try {
/* 203 */           setStringValue(this.rs.getVcalendar(ctxt, null));
/*     */         } catch (IOException e) {
/* 205 */           setStringValue("");
/* 206 */           ZimbraLog.dav.warn("can't get appt data", e);
/*     */         } catch (DavException e) {
/* 208 */           setStringValue("");
/* 209 */           ZimbraLog.dav.warn("can't get appt data", e);
/*     */         }
/* 211 */       return super.toElement(ctxt, parent, nameOnly);
/*     */     }
/*     */   }
/*     */   
/*     */   private static class CalendarHomeSet extends CalDavProperty {
/*     */     public CalendarHomeSet(String user) {
/* 217 */       super();
/* 218 */       String[] homeSets = null;
/*     */       try {
/* 220 */         homeSets = Provisioning.getInstance().getConfig().getMultiAttr("zimbraCalendarCalDavAlternateCalendarHomeSet");
/*     */       }
/*     */       catch (ServiceException se) {}
/*     */       
/* 224 */       if ((homeSets == null) || (homeSets.length == 0)) {
/* 225 */         this.mChildren.add(createHref("/dav/" + user + "/"));
/*     */       } else {
/* 227 */         for (String calHome : homeSets)
/* 228 */           this.mChildren.add(createHref("/dav/" + user + "/" + calHome + "/"));
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private static class ScheduleInboxURL extends CalDavProperty {
/*     */     private final String mUser;
/*     */     
/*     */     public ScheduleInboxURL(String user) {
/* 237 */       super();
/* 238 */       this.mUser = user;
/*     */     }
/*     */     
/*     */     public Element toElement(DavContext ctxt, Element parent, boolean nameOnly)
/*     */     {
/* 243 */       Element inboxUrl = super.toElement(ctxt, parent, nameOnly);
/* 244 */       String authUser = ctxt.getAuthAccount().getName();
/*     */       try {
/* 246 */         authUser = Principal.getOwner(ctxt.getAuthAccount(), ctxt.getUri());
/*     */       }
/*     */       catch (ServiceException se) {}
/* 249 */       inboxUrl.add(createHref(UrlNamespace.getSchedulingInboxUrl(authUser, this.mUser)));
/* 250 */       return inboxUrl;
/*     */     }
/*     */   }
/*     */   
/*     */   private static class ScheduleOutboxURL extends CalDavProperty {
/*     */     private final String mUser;
/*     */     
/* 257 */     public ScheduleOutboxURL(String user) { super();
/* 258 */       this.mUser = user;
/*     */     }
/*     */     
/*     */     public Element toElement(DavContext ctxt, Element parent, boolean nameOnly)
/*     */     {
/* 263 */       Element outboxUrl = super.toElement(ctxt, parent, nameOnly);
/* 264 */       String authUser = ctxt.getAuthAccount().getName();
/*     */       try {
/* 266 */         authUser = Principal.getOwner(ctxt.getAuthAccount(), ctxt.getUri());
/*     */       }
/*     */       catch (ServiceException se) {}
/* 269 */       outboxUrl.add(createHref(UrlNamespace.getSchedulingOutboxUrl(authUser, this.mUser)));
/* 270 */       return outboxUrl;
/*     */     }
/*     */   }
/*     */   
/*     */   private static class CalendarUserAddressSet extends CalDavProperty {
/*     */     public CalendarUserAddressSet(Collection<String> addrs) {
/* 276 */       super();
/* 277 */       for (String addr : addrs) {
/* 278 */         Element e = DocumentHelper.createElement(DavElements.E_HREF);
/* 279 */         if ((!addr.startsWith("http:")) && (!addr.startsWith("mailto:")) && (!addr.startsWith("/")))
/* 280 */           addr = "mailto:" + addr;
/* 281 */         e.setText(addr);
/* 282 */         this.mChildren.add(e);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private static class CalendarFreeBusySet extends CalDavProperty {
/*     */     public CalendarFreeBusySet(String user, Collection<Folder> folders) {
/* 289 */       super();
/* 290 */       setProtected(false);
/* 291 */       setVisible(false);
/*     */       
/* 293 */       ArrayList<Integer> parentIds = new ArrayList();
/*     */       try {
/* 295 */         homeSets = Provisioning.getInstance().getConfig().getMultiAttr("zimbraCalendarCalDavAlternateCalendarHomeSet");
/* 296 */         if (homeSets != null) {
/* 297 */           for (Folder f : folders) {
/* 298 */             String name = f.getName();
/* 299 */             for (String homeset : homeSets) {
/* 300 */               if (name.compareTo(homeset) == 0)
/* 301 */                 parentIds.add(Integer.valueOf(f.getId()));
/*     */             }
/*     */           }
/*     */         }
/*     */       } catch (ServiceException se) {
/*     */         String[] homeSets;
/* 307 */         ZimbraLog.dav.warn("can't generate calendar home set", se);
/*     */       }
/* 309 */       for (Folder f : folders) {
/* 310 */         if (((f.getDefaultView() == MailItem.Type.APPOINTMENT) || (f.getDefaultView() == MailItem.Type.TASK)) && 
/*     */         
/*     */ 
/* 313 */           ((!parentIds.isEmpty()) || (f.getFolderId() == 1)) && 
/*     */           
/*     */ 
/* 316 */           ((parentIds.isEmpty()) || (parentIds.contains(Integer.valueOf(f.getFolderId())))) && 
/*     */           
/*     */ 
/* 319 */           ((f.getFlagBitmask() & Flag.BITMASK_EXCLUDE_FREEBUSY) == 0))
/*     */         {
/*     */ 
/* 322 */           this.mChildren.add(createHref("/dav/" + user + f.getPath() + "/"));
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private static class CalendarUserType
/*     */     extends CalDavProperty
/*     */   {
/*     */     public CalendarUserType(Principal p)
/*     */     {
/* 338 */       super();
/* 339 */       String resType = p.getAccount().getAttr("zimbraCalResType");
/* 340 */       if (resType == null) {
/* 341 */         setStringValue("INDIVIDUAL");
/* 342 */       } else if (resType.compareTo("Equipment") == 0) {
/* 343 */         setStringValue("RESOURCE");
/* 344 */       } else if (resType.compareTo("Location") == 0) {
/* 345 */         setStringValue("ROOM");
/*     */       } else
/* 347 */         setStringValue("UNKNOWN");
/*     */     }
/*     */   }
/*     */   
/*     */   private static class CalendarTimezone extends CalDavProperty {
/*     */     private final Account account;
/*     */     
/* 354 */     public CalendarTimezone(Account a) { super();
/* 355 */       this.account = a;
/*     */     }
/*     */     
/*     */     public Element toElement(DavContext ctxt, Element parent, boolean nameOnly)
/*     */     {
/* 360 */       Element timezone = super.toElement(ctxt, parent, nameOnly);
/* 361 */       ICalTimeZone tz = Util.getAccountTimeZone(this.account);
/* 362 */       CharArrayWriter wr = new CharArrayWriter();
/*     */       try {
/* 364 */         tz.newToVTimeZone().toICalendar(wr, true);
/* 365 */         wr.flush();
/* 366 */         timezone.addCDATA(new String(wr.toCharArray()));
/* 367 */         wr.close();
/*     */       }
/*     */       catch (IOException e) {}
/* 370 */       return timezone;
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/dav/property/CalDavProperty.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */